<?php
namespace app\qihan\model;

use think\Model;
use think\Loader;

class QihanArticle extends Model
{
    public function getContentAttr($value)
    {
        return htmlspecialchars_decode($value);
    }
    //保存
    public function storage($data = [])
    {
        if (!$data) {
            $data = request()->post();
        }
        if(!isset($data['title']) || !$data['title'] || strlen($data['title']) >50 ){
            $this->error = "title不能为空，且长度不能超过50个字符";
            return false;
        }
        //保存
        if (isset($data['id']) && !empty($data['id'])) {
            $res = $this->update($data);
        } else {
            $res = $this->create($data);
        }
        if (!$res) {
            $this->error = '保存失败';
            return false;
        }
        return $res;
    }
    public function category()
    {
        return $this->hasOne('QihanCategory', 'id', 'category_id');
    }
    public function del($ids = '') {
        if (is_array($ids)) {
            $error = '';
            foreach ($ids as $k => $v) {
                $map = [];
                $map['id'] = $v;
                self::where($map)->delete();
            }
            return true;
        }else if($ids) {
            $map = [];
            $map['id'] = $ids;
            self::where($map)->delete();
            return true;
        }
        $this->error = '参数传递错误';
        return false;
    }
}